package com.extension.company.model.my;

import android.util.Log;

import com.extension.company.Constant;
import com.extension.company.contract.AddressListContract;
import com.extension.company.common.http.RetrofitHelper;
import com.extension.company.contract.home.OnlineWaitForPaymentContract;
import com.extension.company.model.api.RetrofitUrlApi;
import com.extension.company.model.entity.AddressListData;
import com.extension.company.model.entity.AddressListDelData;

import org.json.JSONException;
import org.json.JSONObject;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 收货地址
 */
public class AddressListMode implements AddressListContract.Mode {

	private RetrofitUrlApi retrofitUrlApi = RetrofitHelper.getRetrofit().create(RetrofitUrlApi.class);


	/**
	 * 功能：请求列表
	 *
	 * @param callBack
	 */
	@Override
	public void mAddListRequest(final AddressListContract.AddressListCallBack callBack) { // 请求收货地址的接口
		retrofitUrlApi.AddressList(Constant.SIGN, Constant.TOKEN)
				.observeOn(AndroidSchedulers.mainThread())
				.subscribeOn(Schedulers.io())
				.subscribe(new Observer<AddressListData>() {
					@Override
					public void onSubscribe(Disposable d) {

					}

					@Override
					public void onNext(AddressListData addressListData) {
						if (200 == addressListData.getCode()) {
							callBack.AddressListCallBackSuccessful(addressListData.getData().getAddress());  // 请求成功回调给P层
						}

					}

					@Override
					public void onError(Throwable e) {
						/*callBack.AddressListCallBackFaile(e.toString());*/

					}

					@Override
					public void onComplete() {

					}
				});

	}

	/**
	 * 删除某个item的id
	 */
	public void deleteAddressList(String id, final AddressListContract.deleteItemCallBack deleteItemCallBack) {

		retrofitUrlApi.DeleteAddress(Constant.SIGN, Constant.TOKEN, id)
				.observeOn(AndroidSchedulers.mainThread())
				.subscribeOn(Schedulers.io())
				.subscribe(new Observer<AddressListDelData>() {
					@Override
					public void onSubscribe(Disposable d) {

					}

					@Override
					public void onNext(AddressListDelData s) {
						if (200 == s.getCode()) {
							deleteItemCallBack.deleteItem(s.getMsg());
						}
					}

					@Override
					public void onError(Throwable e) {

					}

					@Override
					public void onComplete() {

					}
				});
	}

	/**
	 * 设置默认收货地址的网络
	 *
	 * @param addressId 地址的id
	 */
	public void setDefaultAddress(int addressId, final AddressListContract.defaultAddressCallBack callback) {
		retrofitUrlApi.defaultAddress(Constant.SIGN, Constant.TOKEN, addressId)
				.subscribeOn(Schedulers.io())
				.observeOn(AndroidSchedulers.mainThread())
				.subscribe(new Observer<String>() {
					@Override
					public void onSubscribe(Disposable d) {

					}

					@Override
					public void onNext(String s) {
						try {
							JSONObject jsonObject = new JSONObject(s);
							if ("200".equals(jsonObject.optString("code"))) {
								callback.defaultAddressCallBack(true);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}

					}

					@Override
					public void onError(Throwable e) {

					}

					@Override
					public void onComplete() {

					}
				});
	}

}
